package qumaimai.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import qumaimai.dao.BaseOrderDao;
import qumaimai.dao.CartDao;
import qumaimai.dao.FundDao;
import qumaimai.dao.GoodsDao;
import qumaimai.dao.OrderInfoDao;
import qumaimai.dao.impl.BaseOrderDaoImpl;
import qumaimai.dao.impl.CartDaoImpl;
import qumaimai.dao.impl.FundDaoImpl;
import qumaimai.dao.impl.GoodsDaoImpl;
import qumaimai.dao.impl.OrderInfoDaoImpl;
import qumaimai.entity.BaseOrder;
import qumaimai.entity.OrderInfo;
import qumaimai.entity.User;
import qumaimai.entity.vo.CartVo;
import qumaimai.entity.vo.OrderVo;
import qumaimai.util.JdbcUtil;

@WebServlet("/BaseOrderServlet")
public class BaseOrderServlet extends BaseServlet{
	
	BaseOrderDao dao = new BaseOrderDaoImpl();
	
	
	//添加订单：1、订单表添加记录；2、订单详情表循环添加记录；3、购物车表删除对应记录4、更新购物车session；5、更新库存；6、余额支付
	public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		CartDao cartDao = new CartDaoImpl();
		GoodsDao goodsDao = new GoodsDaoImpl();
		OrderInfoDao infoDao = new OrderInfoDaoImpl();
		FundDao fundDao = new FundDaoImpl();

		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		
		int addressId = Integer.parseInt(request.getParameter("addressId"));
		double money = (double) session.getAttribute("choiceMoney");
		BaseOrder order = new BaseOrder();
		order.setAddressId(addressId);
		order.setUserId(user.getId());
		order.setMoney(money);
		order.setStatus(1);//未付款
		double balance = fundDao.getBlanaceByUserId(user.getId());
		if(balance>money){
			fundDao.pay(money, balance-money, user.getId());
			order.setStatus(2);
		}else{
			order.setStatus(1);//未付款
		}
		
		Connection conn = JdbcUtil.getConnection();
		//设置手动事务
		try {
			conn.setAutoCommit(false);
			int n = dao.add(order);//添加订单
			//查询新添加订单表的记录id
			int id = dao.getId(user.getId());
			
			//从session取出购物车结算时选中的商品列表
			List<CartVo> list = (List<CartVo>) session.getAttribute("cartChoiceList");
			
			//循环添加订单详情
			for(CartVo cart:list){
				OrderInfo info = new OrderInfo(0, id, cart.getGoodsId(), cart.getNum());
				infoDao.add(info);
				//删除购物车表中已下单的数据
				cartDao.del(cart.getId());
				//更新商品库存
				goodsDao.updateStock(cart.getGoodsId(), cart.getNum());
			}
			conn.commit();//提交
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				conn.rollback();//回滚
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		
		//更新购物车session
		List<CartVo> cartList = cartDao.findByUserId(user.getId());
		session.setAttribute("cartList", cartList);
		int totalNum = cartDao.getNum(user.getId());
    	session.setAttribute("totalNum", totalNum);//总件数
    	double totalMoney = 0;
    	for(CartVo vo:cartList){
    		totalMoney+=vo.getPrice()*vo.getNum();
    	}
    	session.setAttribute("totalMoney", totalMoney);//总金额
		response.sendRedirect("buycarthree.jsp");
	}
	
	
	public void findOrderByUserId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		List<BaseOrder> list = dao.findOrderByUserId(user.getId());
		session.setAttribute("orderList", list);//我的订单
		response.sendRedirect("order.jsp");
	}
		public void cancel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			OrderInfoDao orderInfoDao = new OrderInfoDaoImpl();
			GoodsDao goodsDao = new GoodsDaoImpl();
			
			
			int id = Integer.parseInt(request.getParameter("id"));
			int n = dao.cancel(id);
			
			List<OrderVo> list = orderInfoDao.findDetail(id);
			for(OrderVo vo:list){
				goodsDao.addStock(vo.getGoodsId(), vo.getBuyNum());
			}
			findOrderByUserId(request,response);
		}
		
		public void confirm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			int id = Integer.parseInt(request.getParameter("id"));
			int n = dao.confirm(id);
			findOrderByUserId(request,response);
		}
}
